/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.bloginfo.controller;

import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.annotion.Permission;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.modular.bloginfo.param.BlogInfoParam;
import vip.xiaonuo.modular.bloginfo.service.BlogInfoService;

import javax.annotation.Resource;

/**
 * 博客信息表控制器
 *
 * @author 刘文理
 * @date 2021-07-09 09:20:28
 */
@RestController
public class BlogInfoController {

    @Resource
    private BlogInfoService blogInfoService;
    @Resource
    private RedissonClient redissonClient;

    /**
     * 查询博客信息表
     *
     * @author 刘文理
     * @date 2021-07-09 09:20:28
     */
    @Permission
    @GetMapping("/blogInfo/page")
    @BusinessLog(title = "博客信息表_分页", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(BlogInfoParam blogInfoParam) {
        return new SuccessResponseData(blogInfoService.page(blogInfoParam));
    }

    /**
     * 添加博客信息表
     *
     * @author 刘文理
     * @date 2021-07-09 09:20:28
     */
    @Permission
    @PostMapping("/blogInfo/add")
    @BusinessLog(title = "博客信息表_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(BlogInfoParam.add.class) BlogInfoParam blogInfoParam) {
        blogInfoService.add(blogInfoParam);
        return new SuccessResponseData();
    }

    /**
     * 删除博客信息表
     *
     * @author 刘文理
     * @date 2021-07-09 09:20:28
     */
    @Permission
    @PostMapping("/blogInfo/delete")
    @BusinessLog(title = "博客信息表_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(BlogInfoParam.delete.class) BlogInfoParam blogInfoParam) {
        blogInfoService.delete(blogInfoParam);
        return new SuccessResponseData();
        //return new ResponseData(false, 200, "对不起,暂不支持删除!", null);
    }

    /**
     * 编辑博客信息表
     *
     * @author 刘文理
     * @date 2021-07-09 09:20:28
     */
    @Permission
    @PostMapping("/blogInfo/edit")
    @BusinessLog(title = "博客信息表_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(BlogInfoParam.edit.class) BlogInfoParam blogInfoParam) {
        if (blogInfoService.edit(blogInfoParam)) {

            return new SuccessResponseData();
        }
        return new ResponseData(false, 201, "无法修改别人的数据！", null);
    }

    /**
     * 查看博客信息表
     *
     * @author 刘文理
     * @date 2021-07-09 09:20:28
     */
    @Permission
    @GetMapping("/blogInfo/detail")
    @BusinessLog(title = "博客信息表_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(BlogInfoParam.detail.class) BlogInfoParam blogInfoParam) {
        return new SuccessResponseData(blogInfoService.detail(blogInfoParam));
    }

    /**
     * 博客信息表列表
     *
     * @author 刘文理
     * @date 2021-07-09 09:20:28
     */
    @Permission
    @GetMapping("/blogInfo/list")
    @BusinessLog(title = "博客信息表_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(BlogInfoParam blogInfoParam) {
        return new SuccessResponseData(blogInfoService.list(blogInfoParam));
    }

    /**
     * 博客信息表列表
     *
     * @author 刘文理
     * @date 2021-07-09 09:20:28
     */
    @Permission
    @PostMapping("/blogInfo/toggle/blogStatus")
    @BusinessLog(title = "博客信息表_切换状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData blogStatusToggle(@RequestBody @Validated(BlogInfoParam.changeStatus.class) BlogInfoParam blogInfoParam) {
        boolean b = blogInfoService.blogStatusToggle(blogInfoParam);
        return new ResponseData(b, 200, b ? "切换成功!" : "无法修改别人的数据", null);
    }

    /**
     * 博客信息表列表
     *
     * @author 刘文理
     * @date 2021-07-09 09:20:28
     */
    @Permission
    @PostMapping("/blogInfo/toggle/enableComment")
    @BusinessLog(title = "博客信息表_切换状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData enableCommentToggle(@RequestBody @Validated(BlogInfoParam.changeStatus.class) BlogInfoParam blogInfoParam) {
        boolean b = blogInfoService.enableCommentToggle(blogInfoParam);
        return new ResponseData(b, 200, b ? "切换成功!" : "无法修改别人的数据！", null);
    }

    /**
     * 博客信息表列表
     *
     * @author 刘文理
     * @date 2021-07-09 09:20:28
     */
    @Permission
    @PostMapping("/blogInfo/toggle/isDeleted")
    @BusinessLog(title = "博客信息表_切换状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData isDeletedToggle(@RequestBody @Validated(BlogInfoParam.changeStatus.class) BlogInfoParam blogInfoParam) {
        boolean b = blogInfoService.isDeletedToggle(blogInfoParam);
        return new ResponseData(b, 200, b ? "切换成功!" : "无法修改别人的数据！", null);
    }

    @Permission
    @PostMapping("/blogInfo/toggle/isRecommend")
    @BusinessLog(title = "博客信息表_切换状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData isRecommendToggle(@RequestBody @Validated(BlogInfoParam.changeStatus.class) BlogInfoParam blogInfoParam) {
        boolean b = blogInfoService.isRecommendToggle(blogInfoParam);
        return new ResponseData(b, 200, b ? "切换成功!" : "无法修改别人的数据！", null);
    }

    @Permission
    @PostMapping("/blogInfo/toggle/isTop")
    @BusinessLog(title = "博客信息表_切换状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public ResponseData isTopToggle(@RequestBody @Validated(BlogInfoParam.changeStatus.class) BlogInfoParam blogInfoParam) {
        boolean b = blogInfoService.isTopToggle(blogInfoParam);
        return new ResponseData(b, 200, b ? "切换成功!" : "无法修改别人的数据！", null);
    }
    @Permission
    @PostMapping("/blogInfo/deletePageCahe")
    @BusinessLog(title = "博客信息表_删除分页缓存", opType = LogAnnotionOpTypeEnum.OTHER)
    public ResponseData deletePageCahe() {
        boolean b = redissonClient.getBucket("vip.xiaonuo.modular.bloginfo.service.impl.BlogInfoServiceImpl.pubPage").delete();
        return new ResponseData(b, 200, b ? "删除缓存成功!" : "删除缓存失败！", null);
    }
    @Permission
    @PostMapping("/blogInfo/deleteArticleCahe")
    @BusinessLog(title = "博客信息表_删除文章缓存", opType = LogAnnotionOpTypeEnum.OTHER)
    public ResponseData deleteArticleCahe(@RequestBody BlogInfoParam blogInfoParam) {
        redissonClient.getMap("vip.xiaonuo.modular.bloginfo.service.impl.BlogInfoServiceImpl.getOne").remove(String.valueOf(blogInfoParam.getBlogId()));
        return new ResponseData(true, 200, "删除缓存成功!", null);
    }

}
